package com.zero.youxiao.dao;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.zero.basic.dao.DefaultBaseDao;
import com.zero.basic.dao.SqlListBaseDao;
import com.zero.basic.dao.SqlQueryBaseDao;
import com.zero.basic.dao.SqlUpdateBaseDao;
import com.zero.basic.exception.SmsDaoException;
import com.zero.mms.model.Dep;
import com.zero.youxiao.dto.DepDto;

@Repository("depDao")
@Transactional(rollbackFor = Exception.class)
public class DepDaoImpl extends DefaultBaseDao<Dep> implements DepDao {

	@Inject
	private SqlQueryBaseDao<?> sqlQueryBaseDao;

	@Inject
	private SqlListBaseDao<?> sqlListBaseDao;
	@Inject
	private SqlUpdateBaseDao<?> sqlUpdateBaseDao;

	@Override
	public List<Dep> getDepTree(Integer depNo, String depName, String depType)
			throws SmsDaoException {
		try {
			String sql = "select d.dep_id from t_dep d";
			boolean hasWhere = false;
			if (0 != depNo) {
				sql += " where d.dep_no=" + depNo;
				hasWhere = true;
			}
			if (!"".equals(depName)) {
				if (hasWhere)
					sql += " and d.dep_name='" + depName + "'";
				else {
					sql += " where d.dep_name='" + depName + "'";
					hasWhere = true;
				}
			}
			if (!"".equals(depType)) {
				if (hasWhere)
					sql += " and d.dep_type='" + depType + "'";
				else {
					sql += " where d.dep_type='" + depType + "'";
					hasWhere = true;
				}
			}
			if (!hasWhere) {
				sql += " where d.dep_id=1";
			}
			Integer depId = (Integer) sqlQueryBaseDao.queryBySql(sql, null,
					null, false, false);
			if (null == depId)
				return null;

			String reSql = "CALL showDeps(" + depId + ")";
			sqlUpdateBaseDao.updateBySql(reSql);

			String selectSql = "SELECT B.nLevel,A.* "
					+ "FROM t_dep A,tmpLst B  "
					+ "WHERE A.dep_id=B.ID  ORDER BY B.sCort";
			List<Dep> deps = sqlListBaseDao.listBySql(selectSql, Dep.class,
					true, true);
			System.out.println(deps);
			return deps;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsDaoException();
		}
	}

	@Override
	public Integer getMaxLevel() throws SmsDaoException {
		try {
			String selectSql = "select max(tmp.nLevel) as maxLevel from tmpLst tmp";
			Integer depId = (Integer) sqlQueryBaseDao.queryBySql(selectSql,
					null, null, false, false);
			return depId;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsDaoException();
		}
	}

	@Override
	public DepDto loadDepInfo(Integer depId) throws SmsDaoException {
		try {
			String sql = "";
			if (depId == 1)
				sql = "select d.* from t_dep d where d.dep_id = " + depId;
			else
				sql = "select d.*,dp.dep_name as parentName from t_dep d,t_dep dp where d.parent_id = dp.dep_id and d.dep_id = "
						+ depId;
			DepDto deps = (DepDto) sqlQueryBaseDao.queryBySql(sql, null,
					DepDto.class, false, true);
			return deps;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsDaoException();
		}
	}

	@Override
	public void updateDep(Dep dep) throws SmsDaoException {
		try {
			String sql = "update t_dep d set d.dep_no = ?,d.dep_name = ?,d.dep_type=?,d.phone=?,d.dep_tax=?,d.dep_desc=?,d.create_date=? where d.dep_id = ?";
			sqlUpdateBaseDao.updateBySql(
					sql,
					new Object[] { dep.getDepNo(), dep.getDepName(),
							dep.getDepType(), dep.getPhone(), dep.getDepFax(),
							dep.getDepDesc(), dep.getCreateDate(),
							dep.getDepId() });
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsDaoException();
		}
	}

	@Override
	public void deleteDep(Integer depId) throws SmsDaoException {
		try {
			String sql = "delete d.* from t_dep d where d.dep_id = ?";
			sqlUpdateBaseDao.updateBySql(sql, depId);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsDaoException();
		}
	}

}
